﻿/*
 * mkglyph
 */
#include <Windows.h>
#include <stdio.h>

#include "glyph.h"
#include "raster.h"
#include "render_window.h"
#include "glyphtexture.h"
#include "drawtext.h"
#include "drawrect.h"

static int gtid = 101;
static int msgc = 0;
UINT msgs[4];

static
LRESULT CALLBACK
StaticWndProc(
	HWND hWnd, 
	UINT msg, 
	WPARAM wparam, 
	LPARAM lparam
	)
{
	++msgc;

	if (msg == WM_ACTIVATE)
	{
		CREATESTRUCT *cs = (CREATESTRUCT*)lparam;
		int a = 10;
		a++;
	}

	switch (msg)
	{
	case WM_KEYDOWN:
		wparam == VK_DOWN ? ++gtid : --gtid;
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wparam, lparam);
}

static HWND gWnd = NULL;
static wchar_t szClassName[] = L"CLS_w32";

LRESULT CALLBACK CBTProc(
	_In_ int    nCode,
	_In_ WPARAM wParam,
	_In_ LPARAM lParam
)
{
	WCHAR clsn[10];

	if (nCode == HCBT_CREATEWND)
	{
		CBT_CREATEWND* cw = (CBT_CREATEWND*)lParam;
		GetClassName((HWND)wParam, clsn, 10);
	}

	return CallNextHookEx(0, nCode, wParam, lParam);
}

void regcls()
{

	WNDCLASSW wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = StaticWndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = GetModuleHandleW(NULL);

	wc.hIcon = LoadIconW(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(32512));

	wc.lpszMenuName = 0;
	wc.lpszClassName = szClassName;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

	RegisterClassW(&wc);

	RECT rc;
	SetRect(&rc, 0, 0, 800, 600);
	AdjustWindowRect(&rc, WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION, 0);

	HHOOK hook = SetWindowsHookExW(WH_CBT, &CBTProc, GetModuleHandleW(NULL), GetCurrentThreadId());

	HWND hWnd = CreateWindowExW(
		0,
		szClassName,
		L"ddd",
		WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		rc.right - rc.left,
		rc.bottom - rc.top,
		NULL,
		NULL,
		GetModuleHandleW(NULL),
		(LPVOID)1234
	);

	UnhookWindowsHookEx(hook);
	/* */

	UpdateWindow(hWnd);
	ShowWindow(hWnd, SW_SHOW);

	GetClientRect(hWnd, &rc);
	HRESULT hr;
	hr = CreateDevice(hWnd);
	hr = CreateTexture(100, 512, 512);
}

static void DrawScene_String()
{
	ClearScene(255, 255, 255);

	BeginScene();


	WCHAR Str1[] = L"你好，世界。";
	WCHAR Str2[] = L"Hello, world.";
	WCHAR Str3[] = L"VMWA";
	
	DrawRect(0, 100, 512, 512, 0);
	DrawString(0, 60, Str1);
	DrawString(1, 160, Str2);
	DrawString(0, 30, Str3);

	EndScene();

	Present();
}

void OnNullFunc()
{

}

static void InitScene()
{
	//UINT Format = GGO_BITMAP;
	UINT Format = GGO_GRAY8_BITMAP;

	FONTOBJECTPTR FontObj = MakeGdiFont(TEXT("微软雅黑"), 30, Format);
	DWORD cbfont = CalcFontMemSize(FontObj);

	FontInstance.glyphtex = MakeGlyphTexture(FontObj);
	FontInstance.indices = MakeGlyphTextureIndex(FontInstance.glyphtex);

	CreateTextureFromFile(0, 0, 0, L"C:\\Dev\\Xzh\\VivExplorer\\Bin\\res_entry_btn\\entry_btn_0006.png");
}

int RealMain()
{
regcls();

	InitScene();

	MSG msg;
	BOOL ReceiveQuit = FALSE;
	while (!ReceiveQuit)
	{
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			ReceiveQuit = msg.message == WM_QUIT;
			if (!ReceiveQuit)
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				break;
			}
		}

		if (!ReceiveQuit)
		{
			RenderScene(&DrawScene_String, &OnNullFunc, &OnNullFunc);
		}
	}
	return 0;
}

int CALLBACK WinMain(
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR     lpCmdLine,
  int       nCmdShow
)
{
	RealMain();
	return 0;
}

int main(int _Argc, char ** _Argv, char ** _Env)
{	
	RealMain();
	return 0;
}
